﻿$(document).ready(function () {
    page.init();
});

var page = {
    url: "/handler/lottery.ashx?action=",
    pageIndex: 0,
    pageSize: 10,

    init: function () {
        $(".top-bar .right li:eq(4)").addClass("current");
        page.list(0, 0);
    },

    list: function (type, pageIndex, obj) {
        if (pageIndex != undefined)
            page.pageIndex = pageIndex;

        if (obj != undefined) {
            $(".tabs").find("dd").removeClass("active");
            $(obj).parent().addClass("active");
        }

        $.get(page.url + "list", { type: type, pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random() }, function (data) {
            $("#result").html(TrimPath.processDOMTemplate("auctionlist", data));
            page.pager(data.count);
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.history
        });
    },

    joinCount: function (participants) {
        if (participants == undefined)
            return 0;
        var array = participants.split(',');
        var count = 0;
        for (var i = 0; i < array.length; i++) {
            if (array[i] != "")
                count++;
        }
        return count;
    },

    join: function (id, amount) {
        if (confirm("参与该抽奖将扣除" + amount + "素币，确定参与么？")) {
            $.post(page.url + "join", { id: id }, function (data) {
                if (data.error) {
                    alert(data.error);
                    return false;
                }
                page.currentUser.available -= amount;
                $("#participantCount" + id).html(parseInt($("#participantCount" + id).html()) + 1);
                $("#currentAvailable").html(page.currentUser.available);
                $("#buy_button_a" + id).addClass("print").find("a").unbind("click").removeAttr("onclick");
            }, "json");
        }
    },

    open: function (id) {
        window.open("LotteryOpen.aspx?id=" + id);
    }
};

